package com.itfitness.mchart.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewParent;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseImageView;
import com.itfitness.mchart.R;
import com.itfitness.mchart.bean.InviteNotifyBean;
import com.itfitness.mchart.model.dao.InviteNotifyDao;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: MChart
 * @Package: com.itfitness.mchart.activity
 * @ClassName: InvitenotifyActivity
 * @Description: java类作用描述 ：邀请通知页面
 * @Author: 作者名：lml
 * @CreateDate: 2019/4/25 15:57
 * @UpdateUser: 更新者：
 * @UpdateDate: 2019/4/25 15:57
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */

public class InvitenotifyActivity extends BaseActivity {

    @BindView(R.id.activity_invitenotify_rv)
    RecyclerView activityInvitenotifyRv;
    private BaseQuickAdapter<InviteNotifyBean, BaseViewHolder> mBaseQuickAdapter;

    @Override
    protected int LayoutRes() {
        return R.layout.activity_invitenotify;
    }

    @Override
    protected boolean isHaveTitle() {
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initDatas();
    }

    private void initDatas() {
        setTitle("邀请通知", R.color.colorTitleText);
        activityInvitenotifyRv.setLayoutManager(new LinearLayoutManager(this));
        initInvitenotifyList();
    }

    private void initInvitenotifyList() {
        final InviteNotifyDao inviteNotifyDao = new InviteNotifyDao(this);
        List<InviteNotifyBean> inviteNotifyBeans = inviteNotifyDao.selectAllWithUser(EMClient.getInstance().getCurrentUser());
        if (mBaseQuickAdapter == null) {
            mBaseQuickAdapter = new BaseQuickAdapter<InviteNotifyBean, BaseViewHolder>(R.layout.item_invitenotify, inviteNotifyBeans) {
                @Override
                protected void convert(BaseViewHolder helper, final InviteNotifyBean item) {
                    helper.setText(R.id.item_invitenotify_tv_name, item.getInvite());
                    EaseImageView avatat = helper.getView(R.id.avatar);
                    final TextView tv_status = helper.getView(R.id.item_invitenotify_tv_status);
                    final View viewBtContainer = helper.getView(R.id.item_invitenotify_bt_container);
                    viewBtContainer.setVisibility(View.GONE);
                    switch (item.getType()) {
                        case InviteNotifyBean.INVITETYPE_CONTACT:
                            avatat.setImageResource(R.drawable.ease_default_avatar);
                            switch (item.getStatus()) {
                                case CONTACT_CONTACTINVITED:
                                    //收到好友邀请
                                    helper.setText(R.id.item_invitenotify_tv_status, "请求加为好友");
                                    viewBtContainer.setVisibility(View.VISIBLE);
                                    break;
                                case CONTACT_FRIENDREQUESTACCEPTED:
                                    //邀请被同意
                                    helper.setText(R.id.item_invitenotify_tv_status, "对方已同意");
                                    break;
                                case CONTACT_FRIENDREQUESTDECLINED:
                                    //邀请被拒绝
                                    helper.setText(R.id.item_invitenotify_tv_status, "对方已拒绝");
                                    break;
                                case CONTACT_ACCEPTEDFRIENDREQUEST:
                                    //同意好友请求
                                    helper.setText(R.id.item_invitenotify_tv_status, "已同意");
                                    break;
                                case CONTACT_DECLINEDEDFRIENDREQUEST:
                                    //拒绝好友请求
                                    helper.setText(R.id.item_invitenotify_tv_status, "已拒绝");
                                    break;
                            }
                            break;
                        case InviteNotifyBean.INVITETYPE_GROUP:
                            avatat.setImageResource(R.drawable.ease_groups_icon);
                            switch (item.getStatus()) {
                                case GROUP_INVITATIONRECEIVED:
                                    //收到群组邀请
                                    helper.setText(R.id.item_invitenotify_tv_status, "邀您加入群:" + item.getGroupname());
                                    viewBtContainer.setVisibility(View.VISIBLE);
                                    break;
                                case GROUP_INVITATIONACCEPTED:
                                    //群组邀请被同意
                                    helper.setText(R.id.item_invitenotify_tv_status, "对方已同意您入群:"+item.getGroupname());
                                    break;
                                case GROUP_INVITATIONDECLINED:
                                    //群组邀请被拒绝
                                    helper.setText(R.id.item_invitenotify_tv_status, "对方已拒绝您入群:"+item.getGroupname());
                                    break;
                                case GROUP_ACCEPTEDINVITATION:
                                    //同意群组邀请
                                    helper.setText(R.id.item_invitenotify_tv_status, "已同意入群:"+item.getGroupname());
                                    break;
                                case GROUP_DECLINEDINVITATION:
                                    //拒绝群组邀请
                                    helper.setText(R.id.item_invitenotify_tv_status, "已拒绝入群:"+item.getGroupname());
                                    break;
                                case GROUP_AUTOACCEPTINVITATIONFROMGROUP:
                                    //自动同意群组邀请
                                    helper.setText(R.id.item_invitenotify_tv_name, "入群通知");
                                    helper.setText(R.id.item_invitenotify_tv_status, "您已自动加入"+item.getInvite()+"创建的群");
                                    break;
                            }
                            break;
                    }
                    helper.getView(R.id.item_invitenotify_bt_reject).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            decline(viewBtContainer,tv_status,inviteNotifyDao,item);
                        }
                    });
                    helper.getView(R.id.item_invitenotify_bt_agree).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            agree(viewBtContainer,tv_status,inviteNotifyDao,item);
                        }
                    });
                }
            };
            activityInvitenotifyRv.setAdapter(mBaseQuickAdapter);
        }else {
            mBaseQuickAdapter.setNewData(inviteNotifyBeans);
            mBaseQuickAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 拒绝请求
     * @param btcontainer
     * @param tv_status
     * @param inviteNotifyDao
     * @param inviteNotifyBean
     */
    private void decline(final View btcontainer, final TextView tv_status, final InviteNotifyDao inviteNotifyDao, final InviteNotifyBean inviteNotifyBean) {
        switch (inviteNotifyBean.getType()) {
            case InviteNotifyBean.INVITETYPE_CONTACT:
                inviteNotifyBean.setStatus(InviteNotifyBean.InviteNotifyStatus.CONTACT_DECLINEDEDFRIENDREQUEST);
                break;
            case InviteNotifyBean.INVITETYPE_GROUP:
                inviteNotifyBean.setStatus(InviteNotifyBean.InviteNotifyStatus.GROUP_DECLINEDINVITATION);
                break;
        }
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                EMClient.getInstance().contactManager().declineInvitation(inviteNotifyBean.getInvite());
                emitter.onNext(1);
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        if (integer == 1) {
                            inviteNotifyDao.update(inviteNotifyBean);
                            btcontainer.setVisibility(View.GONE);
                            tv_status.setText("已拒绝");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                    }
                });
    }

    /**
     * 同意请求
     * @param btcontainer
     * @param tv_status
     * @param inviteNotifyDao
     * @param inviteNotifyBean
     */
    private void agree(final View btcontainer, final TextView tv_status, final InviteNotifyDao inviteNotifyDao, final InviteNotifyBean inviteNotifyBean) {
        switch (inviteNotifyBean.getType()) {
            case InviteNotifyBean.INVITETYPE_CONTACT:
                inviteNotifyBean.setStatus(InviteNotifyBean.InviteNotifyStatus.CONTACT_ACCEPTEDFRIENDREQUEST);
                break;
            case InviteNotifyBean.INVITETYPE_GROUP:
                inviteNotifyBean.setStatus(InviteNotifyBean.InviteNotifyStatus.GROUP_ACCEPTEDINVITATION);
                break;
        }
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                EMClient.getInstance().contactManager().acceptInvitation(inviteNotifyBean.getInvite());
                emitter.onNext(1);
            }
        }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        if (integer == 1) {
                            inviteNotifyDao.update(inviteNotifyBean);
                            btcontainer.setVisibility(View.GONE);
                            tv_status.setText("已同意");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                    }
                });
    }
}
